package com.newtype.meeting.server.service;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.newtype.meeting.client.model.Account;
import com.newtype.meeting.client.model.Department;
import com.newtype.meeting.client.model.Meeting;
import com.newtype.meeting.client.model.MeetingCategory;
import com.newtype.meeting.client.model.MeetingNotify;
import com.newtype.meeting.client.model.MeetingRecord;
import com.newtype.meeting.client.model.MeetingReport;
import com.newtype.meeting.client.service.AccountService;
import com.newtype.meeting.client.service.DepartmentService;
import com.newtype.meeting.client.service.MeetingCategoryService;
import com.newtype.meeting.client.service.MeetingService;
import com.newtype.meeting.server.PMF;
import com.newtype.meeting.shared.MeetingNotifyDTO;

public class MeetingServiceImpl extends RemoteServiceServlet implements
		MeetingService {
	private static final long serialVersionUID = -6805822655090445047L;

	private MeetingCategoryService categoryService = new MeetingCategoryServiceImpl();
	private AccountService accountService = new AccountServiceImpl();
	private DepartmentService departmentService = new DepartmentServiceImpl();

	/**
	 * 保存会议报告
	 */
	public void saveMeetingReport(MeetingReport meetingReport) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			pm.makePersistent(meetingReport);
		} finally {
			pm.close();
		}
	}

	/**
	 * 保存会议记录
	 */
	public void saveMeetingRecord(MeetingRecord meetingRecord) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			pm.makePersistent(meetingRecord);
		} finally {
			pm.close();
		}
	}

	/**
	 * 保存会议
	 */
	public Meeting saveMeeting(Meeting meeting) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			return pm.makePersistent(meeting);
		} finally {
			pm.close();
		}
	}

	/**
	 * 取消会议
	 */
	public void cancelMeeting(String id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Meeting meeting = pm.getObjectById(Meeting.class, id);
			meeting.setCancelTAG(1);
			pm.makePersistent(meeting);
		} finally {
			pm.close();
		}
	}

	/**
	 * 删除会议
	 */
	public void deleteMeeting(String id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Meeting toDelete = pm.getObjectById(Meeting.class, id);
			pm.deletePersistent(toDelete);
		} finally {
			pm.close();
		}
	}

	/**
	 * 根据ID获取会议信息
	 */
	public Meeting getMeeting(String id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Meeting meeting = new Meeting();
		try {
			meeting = pm.getObjectById(Meeting.class, id);
			if (meeting.getCategoryID() != null) {
				MeetingCategory category = categoryService.getCategory(meeting
						.getCategoryID());
				meeting.setCategoryName(category.getCategoryName());
			}
		} finally {
			pm.close();
		}
		return meeting;
	}

	/**
	 * 查询的会议
	 */
	@SuppressWarnings("unchecked")
	public List<Meeting> listMeeting(String accountID) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		List<Meeting> toReturn = new ArrayList<Meeting>();
		try {
			Query query = pm.newQuery(Meeting.class);
			List<Meeting> result = (List<Meeting>) query.execute();
			toReturn = (List<Meeting>) pm.detachCopyAll(result);
			for (Meeting m : toReturn) {
				m.updateProperties();
			}
		} finally {
			pm.close();
		}
		return toReturn;
	}

	/**
	 * 根据会议ID获取该会议的会议记录
	 */
	@SuppressWarnings("unchecked")
	public List<MeetingRecord> listMeetingRecord(String meetingID) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		List<MeetingRecord> toReturn = new ArrayList<MeetingRecord>();
		try {
			Query query = pm
					.newQuery(MeetingRecord.class, "meetingID==keyword");
			query.declareParameters("String keyword");
			List<MeetingRecord> result = (List<MeetingRecord>) query
					.execute(meetingID);
			toReturn = (List<MeetingRecord>) pm.detachCopyAll(result);
			for (MeetingRecord mr : toReturn) {
				mr.updateProperties();
			}
		} finally {
			pm.close();
		}
		return toReturn;
	}

	/**
	 * 根据会议ID获取该会议的会议报告
	 */
	@SuppressWarnings("unchecked")
	public List<MeetingReport> listMeetingReport(String meetingID) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		List<MeetingReport> toReturn = new ArrayList<MeetingReport>();
		try {
			Query query = pm
					.newQuery(MeetingReport.class, "meetingID==keyword");
			query.declareParameters("String keyword");
			List<MeetingReport> result = (List<MeetingReport>) query
					.execute(meetingID);
			toReturn = (List<MeetingReport>) pm.detachCopyAll(result);
			for (MeetingReport mr : toReturn) {
				mr.updateProperties();
			}
		} finally {
			pm.close();
		}
		return toReturn;
	}

	/**
	 * 根据会议ID删除所有关于该会议的会议记录
	 */
	@SuppressWarnings("unchecked")
	public void deleteAllMeetingRecord(String meetingID) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Query query = pm
					.newQuery(MeetingRecord.class, "meetingID==keyword");
			query.declareParameters("String keyword");
			List<MeetingRecord> result = (List<MeetingRecord>) query
					.execute(meetingID);
			pm.deletePersistentAll(result);
		} finally {
			pm.close();
		}
	}

	/**
	 * 根据会议ID删除所有关于该会议的会议报告
	 */
	@SuppressWarnings("unchecked")
	public void deleteAllMeetingReport(String meetingID) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Query query = pm
					.newQuery(MeetingReport.class, "meetingID==keyword");
			query.declareParameters("String keyword");
			List<MeetingReport> result = (List<MeetingReport>) query
					.execute(meetingID);
			pm.deletePersistentAll(result);
		} finally {
			pm.close();
		}
	}

	/**
	 * 根据会议记录ID删除记录
	 */
	public void deleteMeetingRecord(String id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			MeetingRecord result = pm.getObjectById(MeetingRecord.class, id);
			pm.deletePersistent(result);
		} finally {
			pm.close();
		}
	}

	/**
	 * 根据会议报告ID删除记录
	 */
	public void deleteMeetingReport(String id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			MeetingReport result = pm.getObjectById(MeetingReport.class, id);
			pm.deletePersistent(result);
		} finally {
			pm.close();
		}
	}

	/**
	 * 根据ID获取该会议记录信息
	 */
	public MeetingRecord getMeetingRecord(String id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		return pm.getObjectById(MeetingRecord.class, id);
	}

	/**
	 * 根据ID获取该会议报告信息
	 */
	public MeetingReport getMeetingReport(String id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		return pm.getObjectById(MeetingReport.class, id);
	}

	/**
	 * 获取由我发起的会议
	 */
	@SuppressWarnings("unchecked")
	public List<Meeting> getMeetings(String accountID) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		List<Meeting> toReturn = new ArrayList<Meeting>();
		try {
			Query query = pm.newQuery("SELECT FROM " + Meeting.class.getName()
					+ " WHERE notifierID == keyword || notifierID==keyword");
			query.declareParameters("String keyword");
			List<Meeting> result = (List<Meeting>) query.execute(accountID);
			toReturn = (List<Meeting>) pm.detachCopyAll(result);
			for (Meeting meeting : toReturn) {
				meeting.updateProperties();
			}
		} finally {
			pm.close();
		}
		return toReturn;
	}

	/** 生成会议通知 */
	public void saveMeetingNotify(MeetingNotify notify) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			pm.makePersistent(notify);
		} finally {
			pm.close();
		}
	}

	/**
	 * 通过accountID获取该用户的会议通知
	 */
	@SuppressWarnings("unchecked")
	public List<MeetingNotifyDTO> listMeetingNotice(String accountID) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		List<MeetingNotifyDTO> toReturn = new ArrayList<MeetingNotifyDTO>();

		try {
			Query query = pm.newQuery(MeetingNotify.class,
					"accountID == keyword");
			query.declareParameters("String keyword");
			List<MeetingNotify> result = (List<MeetingNotify>) query
					.execute(accountID);
			for (MeetingNotify mn : result) {
				Meeting meeting = getMeeting(mn.getMeetingID());
				toReturn.add(mn.toLightWeightDTO(meeting));
			}
		} finally {
			pm.close();
		}
		return toReturn;
	}

	/**
	 * 根据Id获取会议通知信息
	 */
	public MeetingNotify getMeetingNotify(String id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		return pm.getObjectById(MeetingNotify.class, id);
	}

	/**
	 * 根据会议类别获取会议集合 
	 */
	@SuppressWarnings("unchecked")
	public List<Meeting> listMeetingByCategory(String categoryID) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		List<Meeting> toReturn = new ArrayList<Meeting>();
		try {
			Query query = pm.newQuery(Meeting.class, "categoryID == keyword");
			query.declareParameters("String keyword");
			List<Meeting> result = (List<Meeting>) query.execute(categoryID);
			toReturn = (List<Meeting>) pm.detachCopyAll(result);
			for (Meeting meeting : toReturn) {
				meeting.updateProperties();
			}
		} finally {
			pm.close();
		}
		return toReturn;
	}

	/**
	 * 根据会议ID获取会议通知者信息
	 */
	@SuppressWarnings("unchecked")
	public List<MeetingNotifyDTO> listMeetingNotify(String meetingID) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		List<MeetingNotifyDTO> toReturn = new ArrayList<MeetingNotifyDTO>();
		try {
			Query query = pm.newQuery(MeetingNotify.class,
					"meetingID == keyword");
			query.declareParameters("String keyword");
			List<MeetingNotify> result = (List<MeetingNotify>) query
					.execute(meetingID);
			for (MeetingNotify notify : result) {
				Account account = accountService.getAccountByAccountID(notify
						.getAccountID());
				Department department = departmentService.getDepartment(account
						.getDeptID());
				MeetingNotifyDTO mn = new MeetingNotifyDTO(notify.getId(),
						notify.getMeetingID(), notify.getAccountID(), notify
								.getAttendTAG(), notify.getReplyContent());
				if (notify.getAttendedTAG() != null
						&& notify.getAttendedTAG() == 1) {
					mn.setAttendedTAG(true);
				} else if (notify.getAttendedTAG() != null
						&& notify.getAttendedTAG() == 0) {
					mn.setAttendedTAG(false);
				} else {
					mn.setAttendedTAG(false);
				}
				mn.setDeptName(department.getDeptName());
				mn.setFullName(account.getFullName());
				toReturn.add(mn);
			}
		} finally {
			pm.close();
		}
		return toReturn;
	}
}
